package bug;

import bug.dao.api.*;
import org.apache.log4j.BasicConfigurator;
import org.hibernate.classic.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor;
import org.springframework.web.context.request.WebRequest;

import java.security.Principal;
import java.util.*;

/**
 * User: ion
 * Date: Aug 30, 2008 - 12:51:29 PM
 */

public class BaseMain {

    protected static Log log = LogFactory.getLog(BaseMain.class);
    protected static ApplicationContext ctx;
    protected static String[] paths = {"bug/applicationContext-DB.xml"};

    protected CategoryDao categoryDao;
    protected UserDao userDao;
    protected ProjectDao projectDao;
    protected UserProjectDao userProjectDao;
    protected IssueDao issueDao;

    private boolean shouldFlush = true;
    protected WebRequest webRequest = new StubWebRequest();
    static {
        ctx = new ClassPathXmlApplicationContext(paths);
        // BasicConfigurator.configure();

        // SessionFactory tr = (SessionFactory)ctx.getBean("sessionFactory");
    }

    public BaseMain() {
        categoryDao = (CategoryDao)ctx.getBean("categoryDAO");
        userDao = (UserDao)ctx.getBean("userDAO");
        projectDao = (ProjectDao)ctx.getBean("projectDAO");
        userProjectDao = (UserProjectDao)ctx.getBean("userProjectDAO");
        issueDao = (IssueDao)ctx.getBean("issueDao");
    }

    protected Session beginSession() {
        Session session = null;
        for (OpenSessionInViewInterceptor interceptor : interceptors()) {
            interceptor.preHandle(webRequest);
            session = interceptor.getSessionFactory().getCurrentSession();
            System.out.println(session.hashCode());
        }
        return session;
    }

    protected void endSession() {
        for (OpenSessionInViewInterceptor interceptor : reverseInterceptors()) {
            System.out.println(interceptor.getSessionFactory().getCurrentSession().hashCode());
            if (shouldFlush) {
                interceptor.postHandle(webRequest, null);
            }
            interceptor.afterCompletion(webRequest, null);
        }
        System.out.println("Session end.");
    }

    private List<OpenSessionInViewInterceptor> interceptors() {
        return Arrays.asList((OpenSessionInViewInterceptor) ctx.getBean("openSessionInViewInterceptor"));
    }

    private List<OpenSessionInViewInterceptor> reverseInterceptors() {
        List<OpenSessionInViewInterceptor> interceptors = interceptors();
        Collections.reverse(interceptors);
        return interceptors;
    }

    public static Log getLog() {
        return log;
    }

    public static void setLog(Log log) {
        BaseMain.log = log;
    }

    public static ApplicationContext getCtx() {
        return ctx;
    }

    public static void setCtx(ApplicationContext ctx) {
        BaseMain.ctx = ctx;
    }

    public static String[] getPaths() {
        return paths;
    }

    public static void setPaths(String[] paths) {
        BaseMain.paths = paths;
    }

    public CategoryDao getCategoryDao() {
        return categoryDao;
    }

    public void setCategoryDao(CategoryDao categoryDao) {
        this.categoryDao = categoryDao;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public ProjectDao getProjectDao() {
        return projectDao;
    }

    public void setProjectDao(ProjectDao projectDao) {
        this.projectDao = projectDao;
    }

    public UserProjectDao getUserProjectDao() {
        return userProjectDao;
    }

    public void setUserProjectDao(UserProjectDao userProjectDao) {
        this.userProjectDao = userProjectDao;
    }

    private static class StubWebRequest implements WebRequest {
        public String getParameter(final String paramName) {
            return null;
        }

        public String[] getParameterValues(final String paramName) {
            return null;
        }

        public Map getParameterMap() {
            return Collections.emptyMap();
        }

        public Locale getLocale() {
            return null;
        }

        public Object getAttribute(final String name, final int scope) {
            return null;
        }

        public void setAttribute(final String name, final Object value, final int scope) {
        }

        public void removeAttribute(final String name, final int scope) {
        }

        public void registerDestructionCallback(final String name, final Runnable callback, final int scope) {
        }

        public String getSessionId() {
            return null;
        }

        public Object getSessionMutex() {
            return null;
        }

		public String getContextPath() {
			// TODO Auto-generated method stub
			return null;
		}

		public String getRemoteUser() {
			// TODO Auto-generated method stub
			return null;
		}

		public Principal getUserPrincipal() {
			// TODO Auto-generated method stub
			return null;
		}

		public boolean isSecure() {
			// TODO Auto-generated method stub
			return false;
		}

		public boolean isUserInRole(String arg0) {
			// TODO Auto-generated method stub
			return false;
		}

        public String getDescription(boolean b) {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public boolean checkNotModified(long l) {
            return false;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public String[] getAttributeNames(int i) {
            return new String[0];  //To change body of implemented methods use File | Settings | File Templates.
        }
    }

}
